<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class auth extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        parent::checkLogin();

    }

    public function levellist()
    {

        $array = array("invalid" => 0);
        $result = $this->db
            ->where($array)
            ->get('sys_level')->result_array();

        $data['data'] = array($result, $_SESSION);
        $this->load->view('head.html', $data);
        $this->load->view('level.html');
        $this->load->view('footer.html');
    }

//新增权限等级
    public function addlevel()
    {
        $type = $this->input->post('type');
        $invalid = $this->input->post('invalid');
        $level = $this->input->post('level');
        $array = array(
            'type' => $type,
            'level' => $level,
        );
        $ret = $this->db->where($array)->get('sys_level')->result_array();

        if ($ret) {
            $this->alert_message(0, '数据已经存在');
        } else {
            $array = array_merge($array, array('invalid' => $invalid, 'time' => time()));
            $ret = $this->db->insert('sys_level', $array);
            if ($ret) {
                $this->alert_message(1, '添加成功', site_url('auth/levellist'));
            } else {
                $this->alert_message(0, '添加失败', site_url('auth/levellist'));
            }
        }
    }

//主要用于模板建跳转
    public function show($view)
    {
        $result = array();
        if ($view == 'addagentlevel.html') {
            $result = $this->db
                ->select("id,level,rebate_rate")
                ->where('type=1')
                ->get('sys_rebate_level')->result_array();
        }
        $data['data'] = array($result, $_SESSION);

        $this->load->view('head.html', $data);
        $this->load->view($view);
        $this->load->view('footer.html');
    }

//管理员列表
    public function userlist()
    {
        $array = array();
        //      $array = array("invalid" => 0);
        $result = $this->db
            ->where($array)
            ->get('sys_admin')->result_array();
        $data['data'] = array($result, $_SESSION);


        $this->load->view('head.html', $data);
        $this->load->view('userlist.html');
        $this->load->view('footer.html');
    }

//新增管理员
    public function adduser()
    {
        $name = $this->input->post('name');
        $array = array(
            'name' => $name,
        );
        $ret = $this->db->where($array)->get('sys_admin')->result_array();
        if ($ret) {
            $this->alert_message(0, '用户已经存在');
        } else {
            $invalid = $this->input->post('invalid');
            $level = $this->input->post('level');
            $pass = $this->input->post('pass');
            $pass1 = $this->input->post('pass2');

            if ($pass != $pass1) {
                $this->alert_message(0, '两次输入的密码不一致');
            } else {
                $array = array_merge($array, array('invalid' => $invalid, 'level' => $level, 'password' => md5($pass)));
                $ret = $this->db->insert('sys_admin', $array);
                if ($ret) {
                    $this->alert_message(1, '添加成功', site_url('auth/userlist'));
                } else {
                    $this->alert_message(0, '添加失败', site_url('auth/userlist'));
                }

            }
        }
    }

//管理权限页面列表
    public function pagelist()
    {
        $array = array('superior_id' => 0);
        //      $array = array("invalid" => 0);
        $result = $this->db
            ->where($array)
            ->get('sys_page')->result_array();

        $data['data'] = array($result, $_SESSION);

        $this->load->view('head.html', $data);
        $this->load->view('pagelist.html');
        $this->load->view('footer.html');
    }

//管理权限页面修改
    public function editlevelpage()
    {
        $id = $this->uri->segment(3);
        $pagename = $this->uri->segment(4);

        $result = array('id' => $id, 'name' => $pagename);
        $data['data'] = array($result, $_SESSION);


        $this->load->view('head.html', $data);
        $this->load->view('editpage.html');
        $this->load->view('footer.html');
    }

//编辑权限管理页面
    public function editpage()
    {
        $id = $this->input->post('id');
        $invalid = $this->input->post('invalid');
        $uid = $this->input->post('uid');
        // $pagename = $this->input->post('pagename');
        $ulevel = $this->input->post('ulevel');

        $array = array(
            'uid' => $uid,
            'invalid' => $invalid,
            'ulevel' => $ulevel
        );
        $ret = $this->db->where('id', $id)->update('sys_page', $array);
        if ($ret) {
            $this->alert_message(1, '配置成功', site_url('auth/pagelist'));
        } else {
            $this->alert_message(0, '配置失败', site_url('auth/pagelist'));
        }
    }

    //个人权限页面配置

    public function pageListInfo()
    {
        $id = $this->uri->segment(3);
        $name = $this->uri->segment(4);
        $resultArr = array('id' => $id, 'name' => $name);

        $result = array();

        $array = array('superior_id !=' => 0);

        $result = $this->db
            ->where($array)
            ->get('sys_page')->result_array();
        $data['data'] = array($result, $_SESSION, $resultArr);
        $this->load->view('head.html', $data);
        $this->load->view('pagelistall.html');
        $this->load->view('footer.html');
    }

    public function doUserPage()
    {
        $id = $this->input->post('adminId');
        $page = $this->input->post('page');

        $pageStr = implode(',', $page);

        $array = array(
            'page' => $pageStr,
        );
        $ret = $this->db->where('id', $id)->update('sys_admin', $array);

        if ($ret) {
            $this->alert_message(1, '配置成功', site_url('auth/userlist'));
        } else {
            $this->alert_message(0, '配置失败', site_url('auth/userlist'));
        }
    }

}